﻿#include "http.h"

HTTP::HTTP()
{

}
bool HTTP::V5getCheckUser(QString pid, const QString pwd, int isFingerprintAuth)
{
    QNetworkAccessManager manager;
    QUrl url(QStringLiteral("%1index.php?r=wsinterface/getCheckUser").arg(config.URL));
    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QByteArray postData;
    postData.append("police_num=").append(pid).append("&");
    postData.append("password=").append(pwd).append("&");
    postData.append("is_fingerprint_auth=").append(QString::number(isFingerprintAuth));
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.1");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }
    //获取响应数据并进行解析。
    QByteArray data = reply->readAll();
    reply->deleteLater();

    QString RespJson = QString::fromUtf8(data);
  config.WriteInfo(RespJson);
    // 将JSON字符串转换为QJsonDocument对象
    QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

    // 检查JSON是否有效
    if (jsonDocument.isNull()) {
        config.WriteInfo("chkUser Failed to parse JSON.");
        return 0;
    }
    // 将QJsonDocument转换为QJsonObject
    QJsonObject jsonObject = jsonDocument.object();

    int result = jsonObject["result"].toInt();
    QString msg = jsonObject["msg"].toString();

    qDebug() << "Result:" << result;
    qDebug() << "Message:" << msg;
    if(result == 0)
    {
        config.WriteInfo("chkUser. 0");

        return false;
    }
    if(result == 1 || result ==2)
    {
        config.WriteInfo("chkUser 1||2");
        return true;
    }


    return false;
}
bool HTTP::V6getCheckUser(QString pid, const QString pwd, int isFingerprintAuth)
{
    if(config.BVersion=="V5")
    {
        if(V5getCheckUser(pid,pwd,isFingerprintAuth))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        QNetworkAccessManager manager;
        QUrl url(QStringLiteral("%1index.php?r=wsinterfaceNew/getCheckUser").arg(config.URL));
        config.WriteInfo(url.toString());
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        QByteArray postData;
        postData.append("police_num=").append(pid).append("&");
        postData.append("password=").append(pwd).append("&");
        postData.append("is_fingerprint_auth=").append(QString::number(isFingerprintAuth));
        //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
        QNetworkReply* reply = manager.post(request, postData);
        QTimer timer;
        timer.setSingleShot(true); // Timer will fire only once
        timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

        // Connect the timeout signal to a slot that will handle the timeout
        QObject::connect(&timer, &QTimer::timeout, [&]() {
            config.WriteInfo("Request timed out.1");
            reply->abort(); // Abort the request if it's taking too long
            return 0;
        });

        // Start the timer
        timer.start();
        while(!reply->isFinished())
        {
            qApp->processEvents();
        }
        //获取响应数据并进行解析。
        QByteArray data = reply->readAll();
        reply->deleteLater();

        QString RespJson = QString::fromUtf8(data);
        config.WriteInfo(RespJson);
        // 将JSON字符串转换为QJsonDocument对象
        QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

        // 检查JSON是否有效
        if (jsonDocument.isNull()) {
            config.WriteInfo("GetUserByPoliceNum Failed to parse JSON.");

            return 0;
        }

        // 将QJsonDocument转换为QJsonObject
        QJsonObject jsonObject = jsonDocument.object();

        // 从QJsonObject中获取特定字段的值
        int code = jsonObject["code"].toInt();
        if(code==100000)
        {
            return true;
        }
    }
    return false;
}
//int CHttpClient::uploadLogCustom( QString fileName,  QString devID,  QString PID,  QString trainsID,  QString trainsGroup)
//{
//    QUrl url(QStringLiteral("%1index.php?r=wsinterface/putUploadLog").arg(URL));
//    config.WriteInfo(url.toString());
//    QString postData = "police_num=" + PID + "&dev_id=" + devID + "&file_name=" + fileName + "&trains_id=" + trainsID + "&train_group_id=" + trainsGroup;

//    QNetworkAccessManager manager;
//    QNetworkRequest request(url);
//    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

//    QNetworkReply* reply = manager.post(request, postData.toUtf8());
//    QTimer timer;
//    timer.setSingleShot(true); // Timer will fire only once
//    timer.setInterval(5000); // Set the timeout interval to 3 seconds (3000 milliseconds)

//    // Connect the timeout signal to a slot that will handle the timeout
//    QObject::connect(&timer, &QTimer::timeout, [&]() {
//        config.WriteInfo("Request timed out.");
//        reply->abort(); // Abort the request if it's taking too long
//        return 0;
//    });

//    // Start the timer
//    timer.start();
//    QEventLoop loop;
//    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
//    loop.exec();

//    QByteArray data = reply->readAll();
//    reply->deleteLater();

//    QString RespJson = QString::fromUtf8(data);
//    config.WriteInfo(RespJson);

//    if (RespJson == "1") // 上传成功
//        return 1;
//    else if (RespJson == "0") // 上传失败
//        return 0;
//    else if (RespJson == "-1") // 上传失败
//        return -1;
//    else
//        return -2;
//}

int HTTP::V5uploadLogCustom(QString fileName, QString devID, QString police_num,QString data_path)
{

    QNetworkAccessManager manager;
    QUrl url(QStringLiteral("%1index.php?r=wsinterface/putUploadLog").arg(config.URL));
    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");


    if(data_path=="")
    {
        data_path=config.UploadUrl;
    }
    QByteArray postData;
    postData.append("police_num=").append(police_num).append("&");//警员编号
    postData.append("dev_id=").append(devID).append("&");//    设备编号
    postData.append("file_name=").append(fileName).append("&");//文件名
    postData.append("data_path=").append(data_path);//盘符
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }
    //获取响应数据并进行解析。
    QByteArray data = reply->readAll();
    reply->deleteLater();

    QString RespJson = QString::fromUtf8(data);
    config.WriteInfo(RespJson);



    if (RespJson == "1") // 上传成功
        return 1;
    else if (RespJson == "0") // 上传失败
        return 0;
    else if (RespJson == "-1") // 上传失败
        return -1;
    else
        return -2;
}
int HTTP::V6uploadLogCustom(QString fileName, QString devID, QString police_num,QString data_path)
{
    if(config.BVersion=="V5")
    {
        if(V5uploadLogCustom(fileName,devID,police_num,data_path)==1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        QNetworkAccessManager manager;
        QUrl url(QStringLiteral("%1index.php?r=wsinterfaceNew/putUploadLog").arg(config.URL));
        config.WriteInfo(url.toString());
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");



        QByteArray postData;
        postData.append("police_num=").append(police_num).append("&");//警员编号
        postData.append("equipment_num=").append(devID).append("&");//    设备编号
        postData.append("file_name=").append(fileName).append("&");//文件名
        postData.append("data_path=").append(data_path);//盘符
        config.WriteInfo(QString("参数%1").arg(QString::fromUtf8(postData)));
        //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
        QNetworkReply* reply = manager.post(request, postData);
        QTimer timer;
        timer.setSingleShot(true); // Timer will fire only once
        timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

        // Connect the timeout signal to a slot that will handle the timeout
        QObject::connect(&timer, &QTimer::timeout, [&]() {
            config.WriteInfo("Request timed out.");
            reply->abort(); // Abort the request if it's taking too long
            return 0;
        });

        // Start the timer
        timer.start();
        while(!reply->isFinished())
        {
            qApp->processEvents();
        }
        //获取响应数据并进行解析。
        QByteArray data = reply->readAll();
        reply->deleteLater();

        QString RespJson = QString::fromUtf8(data);
        config.WriteInfo(RespJson);
        // 将JSON字符串转换为QJsonDocument对象
        QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

        // 检查JSON是否有效
        if (jsonDocument.isNull()) {
            config.WriteInfo("GetUserByPoliceNum Failed to parse JSON.");

            return 0;
        }

        // 将QJsonDocument转换为QJsonObject
        QJsonObject jsonObject = jsonDocument.object();

        // 从QJsonObject中获取特定字段的值
        int code = jsonObject["code"].toInt();
        if(code==100000)
        {
            return 1;
        }
    }
    return 0;
}
int HTTP::V5GetUserByPoliceNum(int index,QString pid, const QString pwd)
{
    QNetworkAccessManager manager;
    QUrl url(QStringLiteral("%1index.php?r=wsinterface/getUserByPoliceNum").arg(config.URL));
    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QByteArray postData;
    postData.append("police_num=").append(pid).append("&");
    postData.append("password=").append(pwd);
    config.WriteInfo(QString("参数%1").arg(QString::fromUtf8(postData)));
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }
    //获取响应数据并进行解析。
    QByteArray data = reply->readAll();
    reply->deleteLater();

    QString RespJson = QString::fromUtf8(data);
    config.WriteInfo(RespJson);
    // 将JSON字符串转换为QJsonDocument对象
    QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

    // 检查JSON是否有效
    if (jsonDocument.isNull()) {
        config.WriteInfo("GetUserByPoliceNum Failed to parse JSON.");

        return 0;
    }

    // 将QJsonDocument转换为QJsonObject
    QJsonObject jsonObject = jsonDocument.object();

    // 从QJsonObject中获取特定字段的值
    int id = jsonObject["id"].toInt();
    QString policeNum = jsonObject["police_num"].toString();
    QString name = jsonObject["name"].toString();
    QString roleId = jsonObject["role_id"].toString();
    QString number = jsonObject["Number"].toString();
    QString loginFailTimes = jsonObject["loginfail_times"].toString();
    QString loginFailLockTime = jsonObject["loginfail_lock_time"].toString();
    QString unit_name = jsonObject["unit_name"].toString();

    if(id==0)
    {
        return 0;
    }
    // 输出解析后的字段值
    qDebug() << "id:" << id;
    qDebug() << "police_num:" << policeNum;
    qDebug() << "name:" << name;
    qDebug() << "role_id:" << roleId;
    qDebug() << "Number:" << number;
    qDebug() << "loginfail_times:" << loginFailTimes;
    qDebug() << "loginfail_lock_time:" << loginFailLockTime;
    qDebug() << "unit_name:" << unit_name;
    if(index>0)
    {
        unitNo[index]=policeNum;
        unitName[index]=unit_name;
        userName[index]=name;
        userNo[index]=pid;
    }
    return 1;

}
int HTTP::V6GetUserByPoliceNum(int index,QString pid, const QString pwd)
{
    if(config.BVersion=="V5")
    {
        if(V5GetUserByPoliceNum(index,pid,pwd)==1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        QNetworkAccessManager manager;
        QUrl url(QStringLiteral("%1index.php?r=wsinterfaceNew/getUserByPoliceNum").arg(config.URL));
        config.WriteInfo(url.toString());
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        QByteArray postData;
        postData.append("police_num=").append(pid).append("&");
        postData.append("password=").append(pwd);
        config.WriteInfo(QString("参数%1").arg(QString::fromUtf8(postData)));
        //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
        QNetworkReply* reply = manager.post(request, postData);
        QTimer timer;
        timer.setSingleShot(true); // Timer will fire only once
        timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

        // Connect the timeout signal to a slot that will handle the timeout
        QObject::connect(&timer, &QTimer::timeout, [&]() {
            config.WriteInfo("Request timed out.");
            reply->abort(); // Abort the request if it's taking too long
            return 0;
        });

        // Start the timer
        timer.start();
        while(!reply->isFinished())
        {
            qApp->processEvents();
        }
        //获取响应数据并进行解析。
        QByteArray data = reply->readAll();
        reply->deleteLater();

        QString RespJson = QString::fromUtf8(data);
        config.WriteInfo(RespJson);
        // 将JSON字符串转换为QJsonDocument对象
        QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

        // 检查JSON是否有效
        if (jsonDocument.isNull()) {
            config.WriteInfo("GetUserByPoliceNum Failed to parse JSON.");

            return 0;
        }

        // 将QJsonDocument转换为QJsonObject
        QJsonObject jsonObject = jsonDocument.object();
        int code = jsonObject["code"].toInt();
        QString msg = jsonObject["msg"].toString();
        QJsonObject parsedAddress = jsonObject["data"].toObject();
        // 从QJsonObject中获取特定字段的值
        int id = parsedAddress["id"].toInt();
        QString policeNum = parsedAddress["police_num"].toString();
        QString name = parsedAddress["name"].toString();
        QString roleId = parsedAddress["role_id"].toString();
        QString number = parsedAddress["number"].toString();
        QString loginFailTimes = parsedAddress["loginfail_times"].toString();
        QString loginFailLockTime = parsedAddress["loginfail_lock_time"].toString();
        QString unit_name = parsedAddress["unit_name"].toString();
        if(index>0)
        {
            unitNo[index]=number;
            unitName[index]=unit_name;
            userName[index]=name;
            userNo[index]=pid;
        }
        if(code==100000)
        {
            return 1;
        }
        // 输出解析后的字段值
        qDebug() << "id:" << id;
        qDebug() << "police_num:" << policeNum;
        qDebug() << "name:" << name;
        qDebug() << "role_id:" << roleId;
        qDebug() << "Number:" << number;
        qDebug() << "loginfail_times:" << loginFailTimes;
        qDebug() << "loginfail_lock_time:" << loginFailLockTime;
        qDebug() << "unit_name:" << unit_name;
    }
    return 0;

}
int HTTP::GetNotice()
{
    try {
           QNetworkAccessManager manager;
           QUrl url( QStringLiteral("%1index.php?r=wsinterface/Notice").arg(config.URL));
           config.WriteInfo(url.toString());
           QNetworkRequest request(url);
           request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

           QByteArray postData;
           postData.append("");
           //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
           QNetworkReply* reply = manager.post(request, postData);
           QTimer timer;
           timer.setSingleShot(true); // Timer will fire only once
           timer.setInterval(3000); // Set the timeout interval to 3 seconds (3000 milliseconds)

           // Connect the timeout signal to a slot that will handle the timeout
           QObject::connect(&timer, &QTimer::timeout, [&]() {
               config.WriteInfo("Request timed out.");
               reply->abort(); // Abort the request if it's taking too long
               return false;
           });

           // Start the timer
           timer.start();
           while(!reply->isFinished())
           {
               qApp->processEvents();
           }

           //获取响应数据并进行解析。
           QByteArray data = reply->readAll();
           reply->deleteLater();

           QString RespJson = QString::fromUtf8(data);
           config.WriteInfo(RespJson);
           if(RespJson=="[]")
           {
               config.WriteInfo("公告信息为[].");
               return true;
           }
           QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

           if (!jsonDocument.isArray()) {
               config.WriteInfo("GetNotice Failed to parse JSON.");
               return false;
           }

           QJsonArray jsonArray = jsonDocument.array();

           if (jsonArray.isEmpty()) {
               config.WriteInfo("GetNotice JSON array is empty.");
               return false;
           }

           Notice.clear();
           // 获取当前时间
           QDateTime currentDateTime = QDateTime::currentDateTime();

           // 将当前时间转换为时间戳
           qint64 timestamp = currentDateTime.toSecsSinceEpoch();

           // 打印时间戳
           qDebug() << "当前时间戳：" << timestamp;
           // 遍历数组中的每个元素
           for (const QJsonValue &value : jsonArray) {
               // 检查元素是否是对象
               if (value.isObject()) {
                   // 获取对象
                   QJsonObject jsonObject = value.toObject();

//                   if(BVersion=="V5")
//                   {
//                       int id = jsonObject.value("id").toInt();
//                       QString title = jsonObject.value("title").toString();
//                       QString content = jsonObject.value("content").toString();
//                       int created_date= jsonObject.value("created_date").toInt();

//                       QString unitNumber = jsonObject.value("unit_number").toString();
//                       QString publisher = jsonObject.value("publisher").toString();
//                       QString publishTime = jsonObject.value("publish_time").toString();
//                       QString deadline = jsonObject.value("deadline").toString();
//                       int type = jsonObject.value("type").toInt();
//                       int status = jsonObject.value("status").toInt();
//                       QDateTime dateTime = QDateTime::fromSecsSinceEpoch(publishTime.toInt());

//                       Announcement announcement;
//                       announcement.AnnouncementCc=publisher;
//                       announcement.AnnouncementTime=dateTime.toString("yyyy-MM-dd HH:mm:ss");
//                       announcement.AnnouncementContent=content;
//                       announcement.AnnouncementTitle=title;
//                       config.WriteInfo(QString("deadline= %1,timestamp=%2").arg(deadline).arg(timestamp));
//                       config.WriteInfo(QString("dateTime= %1").arg(dateTime.toString("yyyy-MM-dd HH:mm:ss")));

//                       if(deadline.toInt()>timestamp)
//                       {
//                           Notice.push_back(announcement);
//                       }
//                   }
//                   else
//                   {
                       int id = jsonObject.value("id").toInt();
                       QString title = jsonObject.value("title").toString();
                       QString content = jsonObject.value("content").toString();
                       int created_date= jsonObject.value("created_date").toInt();

                       QString unitNumber = jsonObject.value("unit_number").toString();
                       QString publisher = jsonObject.value("publisher").toString();
                       int publishTime = jsonObject.value("publish_time").toInt();
                       int deadline = jsonObject.value("deadline").toInt();
                       int type = jsonObject.value("type").toInt();
                       int status = jsonObject.value("status").toInt();
                       QDateTime dateTime = QDateTime::fromSecsSinceEpoch(publishTime);

                       Announcement announcement;
                       announcement.AnnouncementCc=publisher;
                       announcement.AnnouncementTime=dateTime.toString("yyyy-MM-dd HH:mm:ss");
                       announcement.AnnouncementContent=content;
                       announcement.AnnouncementTitle=title;
                       config.WriteInfo(QString("deadline= %1,timestamp=%2").arg(deadline).arg(timestamp));
                       if(deadline>timestamp)
                       {
                           Notice.push_back(announcement);
                       }
                   //}


               }
           }
           return true;
       } catch (const std::exception& e) {
           config.WriteInfo(QString("GetNotice:错误= %1").arg(e.what()));
           return false;
       }

}

int HTTP::GetZFYInfo(int index,QString str)
{
    QNetworkAccessManager manager;
    QUrl url( QStringLiteral("%1index.php?r=wsinterface/getZFYInfo").arg(config.URL));
    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QByteArray postData;
    postData.append("zfyinfo=").append(str);
        config.WriteInfo(postData);
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }

    //获取响应数据并进行解析。
    QByteArray data = reply->readAll();
    reply->deleteLater();

    QString RespJson = QString::fromUtf8(data);
    config.WriteInfo(RespJson);
    if(RespJson=="")
    {
        qDebug() << "请求失败";
        return 0;

    }
    QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

    if (!jsonDocument.isArray()) {
        config.WriteInfo("GetNotice Failed to parse JSON.");
        // 找到等号的位置
        int equalIndex = RespJson.indexOf('=');

        // 检查是否找到等号
        if (equalIndex != -1) {
            // 截取等号后面的字符串
            QString afterEqual = RespJson.mid(equalIndex + 1);

            // 使用逗号分割截取的字符串
            QStringList list = afterEqual.split(',');

            // 检查是否有足够的元素
            if (list.size() >= 2) {
                cSerial[index]=list.at(0);//执法仪序列号
                userNo[index]=list.at(1); //执法记录仪警员编号
                userName[index] = list.at(2); //执法记录仪警员名称
                unitNo[index] = list.at(3);       //执法记录仪单位编号
                unitName[index] = list.at(4);  //执法记录仪单位名称
                status[index] = list.at(5);   //执法仪禁用状态
                bindStatus[index] = list.at(6);//执法仪配对状态；
                watermark[index] = list.at(7);//水印；
                resolution[index] = list.at(8);//分辨率；
                // 输出第三个和第四个逗号之间的字符
                config.WriteInfo(QString("第一个逗号后的字符串: %1").arg(list.at(0)));
                config.WriteInfo(QString("第二个逗号后的字符串: %1").arg(list.at(1)));
                config.WriteInfo(QString("第三个逗号后的字符串: %1").arg(list.at(2)));
                config.WriteInfo(QString("第四个逗号后的字符串: %1").arg(list.at(3)));
                config.WriteInfo(QString("第五个逗号后的字符串: %1").arg(list.at(4)));
                config.WriteInfo(QString("第六个逗号后的字符串: %1").arg(list.at(5)));
            } else {
                config.WriteInfo("逗号分割后的元素不足");
            }
        } else {
            config.WriteInfo("没有找到等号");
        }

        if(status[index]=="2")
        {
            return 2;
        }
        if(status[index]=="1")
        {
            return 1;
        }
        return 1;


    }
}


int HTTP::V6ReportClientVersion(QString version)


{

try{

QNetworkAccessManager manager;
QUrl url(QStringLiteral("%1index.php?r=wsinterface/reportClientVersion").arg(config.URL));
config.WriteInfo(url.toString());
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

QByteArray postData;
postData.append("version=").append(version);//警员编号
config.WriteInfo(QString("参数%1").arg(QString::fromUtf8(postData)));

// 创建QNetworkAccessManager对象 manager，并使用 post 方法发送 POST 请求，并等待请求结束。
QNetworkReply* reply = manager.post(request,postData);
QTimer timer;
timer.setSingleShot(true); // Timer will fire only once
timer.setInterval(20000); // Set the timeout interval to 3 seconds (3000 milliseconds)

// Connect the timeout signal to a slot that will handle the timeout
QObject::connect(&timer, &QTimer::timeout, [&]() {
config.WriteInfo("V5uploadLogCustom 请求超时");
reply->abort(); // Abort the request if it's taking too long
return 0;
});

// Start the timer
timer.start();
while(!reply->isFinished())
{
qApp->processEvents();
}
//获取响应数据并进行解析。
QByteArray data = reply->readAll();
reply->deleteLater();

QString RespJson = QString::fromUtf8(data);
config.WriteInfo(RespJson);



if (RespJson == "1") // 上传成功
return 1;
else if (RespJson == "0") // 上传失败
return 0;
else if (RespJson == "-1") // 上传失败
return -1;
else
return -2;
} catch (const std::exception& e) {
config.WriteInfo(QString("V6ReportClientVersion:错误= %1").arg(e.what()));
return 0;
}

}
int HTTP::GetPlatformInfo()
{

    QNetworkAccessManager manager;
       QUrl url;
    if(config.BVersion=="V5")
    {
       url=(QStringLiteral("%1index.php?r=wsinterface/GetPlatformInfo").arg(config.URL));
    }
    else
    {

        url=(QStringLiteral("%1index.php?r=wsinterfaceNew/GetPlatformInfo").arg(config.URL));
    }

    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QByteArray postData;
    postData.append("");
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }
    //获取响应数据并进行解析。
    QByteArray data = reply->readAll();
    reply->deleteLater();

    QString RespJson = QString::fromUtf8(data);
    config.WriteInfo(RespJson);
    // 将JSON字符串转换为QJsonDocument对象
    QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

    // 检查JSON是否有效
    if (jsonDocument.isNull()) {
        config.WriteInfo("GetUserByPoliceNum Failed to parse JSON.");

        return 0;
    }
    // 将QJsonDocument转换为QJsonObject
    QJsonObject jsonObject = jsonDocument.object();
    int code = jsonObject["code"].toInt();
    QString msg = jsonObject["msg"].toString();
    QJsonObject parsedAddress = jsonObject["data"].toObject();
    // 从QJsonObject中获取特定字段的值
    cloudIp = parsedAddress["cloudIp"].toString();
    station_number = parsedAddress["station_number"].toString();
    access_key = parsedAddress["access_key"].toString();
    // 输出解析后的字段值
    qDebug() << "cloudIp:" << cloudIp;
    qDebug() << "station_number:" << station_number;
    qDebug() << "access_key:" << access_key;

    if(code==100000)
    {
        return 1;
    }



    return 0;
}
//获取文件类型，获取部门
bool HTTP::GetClassifcationType()
{

    //    QString unicodeString = "\\u5e7f\\u4e1c\\u901a\\u521b\\u8054\\u65b0\\u6280\\u672f\\u6709\\u9650\\u516c\\u53f8";

    //      // 将Unicode转义序列转换为中文字符串
    //      QString chineseString = QString::fromUtf8(QByteArray::fromStdString(unicodeString.toStdString()));
    Filetype.clear();
    DepartmentName.clear();
    // 输出结果
    QNetworkAccessManager manager;
    QUrl url( QStringLiteral("http://%1/tsds-api/getcategorylist.do?").arg(cloudIp));
    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QByteArray postData;
    postData.append("");
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }

    //获取响应数据并进行解析
    QByteArray data = reply->readAll();
    reply->deleteLater();
    QString RespJson = QString::fromUtf8(data);
    config.WriteInfo(RespJson);
    QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

    // 检查JSON是否有效
    if (jsonDocument.isNull()) {
        config.WriteInfo("GetClassifcationType Failed to parse JSON.");
        return 0;
    }

    // 将QJsonDocument转换为QJsonObject
    QJsonObject jsonObject = jsonDocument.object();
    int code = jsonObject["code"].toInt();
    QString msg = jsonObject["msg"].toString();
    QJsonObject parsedAddress = jsonObject["data"].toObject();
    // 从QJsonObject中获取特定字段的值"2023-10-24 00:00:00"
    // Extract values from the "data" object
    QJsonArray categoryList = parsedAddress["category_list"].toArray();
    QJsonArray unitList = parsedAddress["unit_list"].toArray();

    // Now, you can iterate over the arrays to access individual elements
    for (const QJsonValue& category : categoryList) {
        QJsonObject categoryObject = category.toObject();
        DepartmentInfo DInfo;

        DInfo.ID = QString::number(categoryObject["id"].toInt());
        DInfo.Name = categoryObject["cate_name"].toString();

        Filetype.push_back(DInfo);
        config.WriteInfo(QString("categoryId:=%1,categoryName:=%2").arg(DInfo.ID).arg(DInfo.Name));
        // Process or store the category data as needed
    }

    for (const QJsonValue& unit : unitList) {
        QJsonObject unitObject = unit.toObject();
        int unitId = unitObject["id"].toInt();
        QString unitName = unitObject["name"].toString();
        QString number = unitObject["number"].toString();
        QString pre_number = unitObject["pre_number"].toString();
        QString pid = unitObject["pid"].toString();
        QString unit_type = unitObject["unit_type"].toString();
        QString description = unitObject["description"].toString();
        QString contact = unitObject["contact"].toString();
        QString phone = unitObject["phone"].toString();
        QString level = unitObject["level"].toString();
        QString updated_at = unitObject["updated_at"].toString();
        QString created_at = unitObject["created_at"].toString();
        QString jz_unit_number = unitObject["jz_unit_number"].toString();
        QString team_data_number = unitObject["team_data_number"].toString();
        QString team_data_upload = unitObject["team_data_upload"].toString();
        QString hongwan_upload = unitObject["hongwan_upload"].toString();
        QString pre_name = unitObject["pre_name"].toString();
        DepartmentInfo DInfo;
        DInfo.ID=number;
        DInfo.Name=unitName;
        DepartmentName.push_back(DInfo);
        config.WriteInfo(QString("number:=%1,unitName:=%2,pre_number:=%3,level:=%4").arg(number).arg(unitName).arg(pre_number).arg(level));
        // Process or store the unit data as needed
    }


    // 输出解析后的字段值
    //    qDebug() << "category_list:" << category_list;
    //    qDebug() << "unit_list:" << unit_list;

    if(code==200)
    {
        return 1;
    }
    return 0;


}
//获取警情
bool HTTP::GetWarnInstanceInfo(QString userID,QString divisionID,QString StartTime,QString EndTime)
{

    //    QString unicodeString = "\\u5e7f\\u4e1c\\u901a\\u521b\\u8054\\u65b0\\u6280\\u672f\\u6709\\u9650\\u516c\\u53f8";

    //      // 将Unicode转义序列转换为中文字符串
    //      QString chineseString = QString::fromUtf8(QByteArray::fromStdString(unicodeString.toStdString()));
    PoliceInfo.clear();
    QDateTime startTime=QDateTime::fromString(StartTime,"yyyyMMddHHmmss");
    qDebug()<<startTime.toString("yyyy-MM-dd HH:mm:ss");
    QDateTime endTime=QDateTime::fromString(EndTime,"yyyyMMddHHmmss");
    qDebug()<<endTime.toString("yyyy-MM-dd HH:mm:ss");
    // 输出结果
    QNetworkAccessManager manager;
    QUrl url( QStringLiteral("http://%1/tsds-api/getcaselist.do?").arg(cloudIp));
    config.WriteInfo(url.toString());
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QByteArray postData;
    postData.append("authKey=").append(access_key).append("&");//工作站秘钥
    postData.append("police_number=").append(userID).append("&");//警员编号
    postData.append("happen_time_start=").append(startTime.toString("yyyy-MM-dd HH:mm:ss")).append("&"); //开始时间
    postData.append("happen_time_end=").append(endTime.toString("yyyy-MM-dd HH:mm:ss")).append("&");//结束时间
    postData.append("station_number=").append(station_number).append("&");//工作站编号
    postData.append("unit_number=").append(divisionID);//部门编号
    qDebug()<<QString::fromUtf8(postData);
    //创建QNetworkAccessManager对象manager，并使用post方法发送POST请求，并等待请求结束。
    QNetworkReply* reply = manager.post(request, postData);
    QTimer timer;
    timer.setSingleShot(true); // Timer will fire only once
    timer.setInterval(10000); // Set the timeout interval to 3 seconds (3000 milliseconds)

    // Connect the timeout signal to a slot that will handle the timeout
    QObject::connect(&timer, &QTimer::timeout, [&]() {
        config.WriteInfo("Request timed out.");
        reply->abort(); // Abort the request if it's taking too long
        return 0;
    });

    // Start the timer
    timer.start();
    while(!reply->isFinished())
    {
        qApp->processEvents();
    }

    //获取响应数据并进行解析
    QByteArray data = reply->readAll();
    reply->deleteLater();
    QString RespJson = QString::fromUtf8(data);
    config.WriteInfo(RespJson);
    QJsonDocument jsonDocument = QJsonDocument::fromJson(RespJson.toUtf8());

    // 检查JSON是否有效
    if (jsonDocument.isNull()) {
        config.WriteInfo("GetClassifcationType Failed to parse JSON.");
        return 0;
    }

    // 将QJsonDocument转换为QJsonObject
    QJsonObject jsonObject = jsonDocument.object();
    int code = jsonObject["code"].toInt();
    QString msg = jsonObject["msg"].toString();
    QJsonObject dataObject = jsonObject["data"].toObject();
    QJsonArray dataList = dataObject["list"].toArray();
    for (const auto& entry:dataList) {
        QJsonObject parsedAddress=entry.toObject();
        // 从QJsonObject中获取特定字段的值
        QString id = parsedAddress["id"].toString();//警情号
        QString case_number = parsedAddress["case_number"].toString();//警情号
        QString unit_number = parsedAddress["unit_number"].toString();//部门号
        QString case_desc = parsedAddress["case_desc"].toString();//警情内容
        QString happen_address = parsedAddress["happen_address"].toString();//警情地点
        QString call_police_time = parsedAddress["happen_time_text"].toString();//出警时间
        QString alarm_time_text = parsedAddress["alarm_time_text"].toString();//处警时间
        QString trafficCaseTable = parsedAddress["trafficCaseTable"].toString();//处警时间

        // 输出解析后的字段值
        qDebug() << "case_number:" << case_number;
        qDebug() << "unit_number:" << unit_number;
        qDebug() << "case_desc:" << case_desc;
        qDebug() << "happen_address:" << happen_address;
        qDebug() << "call_police_time:" << call_police_time;
        qDebug() << "alarm_time_text:" << alarm_time_text;
        qDebug() << "trafficCaseTable:" << trafficCaseTable;

        WarnInfo WInfo;
        WInfo.case_number=case_number;
        WInfo.unit_number=unit_number;
        WInfo.case_desc=case_desc;
        WInfo.happen_address=happen_address;
        WInfo.call_police_time=call_police_time;
        WInfo.alarm_time_text=alarm_time_text;
        WInfo.trafficCaseTable=trafficCaseTable;
        PoliceInfo.push_back(WInfo);

    }


    if(code==100000)
    {
        return 1;
    }



    //    // 从QJsonObject中获取特定字段的值
    //    // Extract values from the "data" object
    //    QJsonArray categoryList = parsedAddress["category_list"].toArray();
    //    QJsonArray unitList = parsedAddress["unit_list"].toArray();

    //    // Now, you can iterate over the arrays to access individual elements
    //    for (const QJsonValue& category : categoryList) {
    //        QJsonObject categoryObject = category.toObject();
    //        int categoryId = categoryObject["id"].toInt();
    //        QString categoryName = categoryObject["cate_name"].toString();
    //        Filetype.push_back(categoryName);
    //        config.WriteInfo(QString("categoryId:=%1,categoryName:=%2").arg(categoryId).arg(categoryName));
    //        // Process or store the category data as needed
    //    }

    //    for (const QJsonValue& unit : unitList) {
    //        QJsonObject unitObject = unit.toObject();
    //        int unitId = unitObject["id"].toInt();
    //        QString unitName = unitObject["name"].toString();
    //                DepartmentName.push_back(unitName);
    //        config.WriteInfo(QString("unitId:=%1,unitName:=%2").arg(unitId).arg(unitName));
    //        // Process or store the unit data as needed
    //    }


    //    // 输出解析后的字段值
    //    //    qDebug() << "category_list:" << category_list;
    //    //    qDebug() << "unit_list:" << unit_list;

    //    if(code==200)
    //    {
    //        return 1;
    //    }
    return 0;


}
